﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;

	public class UserRoleService : IUserRoleService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserRoleService"/> class.
		/// </summary>
		/// <param name="userRoleRepository">The user role repository.</param>
		public UserRoleService(IUserRoleRepository userRoleRepository)
		{
			this.UserRoleRepository = userRoleRepository;
		}

		/// <summary>
		/// Gets or sets the user role repository.
		/// </summary>
		/// <value>The user role repository.</value>
		public IUserRoleRepository UserRoleRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the user role by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="UserRoleSpecification"/>.</param>
		/// <returns></returns>
		public UserRole GetBy(UserRoleSpecification specification)
		{
			return this.UserRoleRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the user role by the specified id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public UserRole GetById(int id)
		{
			return this.UserRoleRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the user roles.
		/// </summary>
		/// <returns></returns>
		public IList<UserRole> GetAll()
		{
			return this.UserRoleRepository.GetAll();
		}

		/// <summary>
		/// Creates the user role.
		/// </summary>
		/// <returns></returns>
		public UserRole Create()
		{
			return new UserRole();
		}

		/// <summary>
		/// Inserts the specified role.
		/// </summary>
		/// <param name="role">The <see cref="UserRole"/>.</param>
		public void Insert(UserRole role)
		{
			role.Validate();

			role.Slug = role.Title.Slug();

			this.UserRoleRepository.Insert(role);
		}

		/// <summary>
		/// Inserts the specified role.
		/// </summary>
		/// <param name="role">The role.</param>
		/// <param name="value">The value.</param>
		public void Insert(UserRole role, UserRoleCreate value)
		{
			value.Validate();

			role.Title = value.Title;

			this.Insert(role);
		}

		/// <summary>
		/// Updates the specified role.
		/// </summary>
		/// <param name="role">The <see cref="UserRole"/>.</param>
		public void Update(UserRole role)
		{
			role.Validate();

			role.Slug = role.Title.Slug();

			this.UserRoleRepository.Update(role);
		}

		/// <summary>
		/// Updates the specified role.
		/// </summary>
		/// <param name="role">The role.</param>
		/// <param name="value">The value.</param>
		public void Update(UserRole role, UserRoleUpdate value)
		{
			value.Validate();

			role.Title = value.Title;

			this.Update(role);
		}

		/// <summary>
		/// Deletes the specified role.
		/// </summary>
		/// <param name="role">The role.</param>
		public void Delete(UserRole role)
		{
			this.UserRoleRepository.Delete(role);
		}
	}
}